import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import styleImport from 'vite-plugin-style-import';
import eslintPlugin from 'vite-plugin-eslint';
import Components from 'unplugin-vue-components/vite';
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers';
// path模块是node.js内置的功能，但是node.js本身并不支持typescript
// 需要yarn add @types/node --save-dev
import { resolve } from 'path';
// const title: string = process.env.VUE_APP_TITLE || '周天的白日梦'; // 网页标题
const port = 9708; // 端口
// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    styleImport({
      libs: [
        {
          libraryName: 'element-plus',
          base: 'element-plus/lib/theme-chalk/base.css',
          resolveStyle: (name) => {
            return `element-plus/lib/theme-chalk/${name}.css`;
          },
          resolveComponent: (name) => {
            return `element-plus/lib/${name}`;
          }
        }
      ]
    }),
    Components({
      resolvers: [ElementPlusResolver()]
    }),
    eslintPlugin({
      include: ['src/**/*.vue', 'src/**/*.js', 'src/**/*.ts'] // 检查的文件
    })
  ],
  base: './',
  build: {
    outDir: 'dist', // 打包名称
    assetsDir: 'static', //静态资源存放路径
    sourcemap: false, // 是否构建sourcemap文件
    // lintOnSave: process.env.NODE_ENV === 'development',
    terserOptions: {
      // 生产环境移除console debugger
      compress: {
        drop_console: true,
        drop_debugger: true
      }
    }
  },
  server: {
    https: false, // 是否开启https
    open: true, // 是否自动浏览器打开
    port: port, // 端口号
    host: '0.0.0.0',
    // 代理
    proxy: {
      '/api': {
        target: '', // 后台地址
        changeOrigin: true,
        secure: false, // 如果是https接口,需要配置该参数
        ws: false, // websocket支持
        rewrite: (path) => path.replace(/^\/api/, '')
      }
    }
  },
  resolve: {
    alias: {
      '@': resolve(__dirname, 'src'),
      '@assets': resolve(__dirname, 'src/assets'),
      '@components': resolve(__dirname, 'src/components'),
      '@views': resolve(__dirname, 'src/views'),
      '@store': resolve(__dirname, 'src/store'),
      '@styles': resolve(__dirname, 'src/styles')
    }
  },
  // 第三方配置
  optimizeDeps: {
    include: []
  }
});
